/**
 * 关于接口请求的一些封装
 */

/**
 * group by
 */
export function grbCondition(code, value) {
  return {
    code,
    operator: 'GRB',
    value
  }
}

/**
 * limit
 */
export function limCondition(code, value) {
  return {
    code,
    operator: 'LIM',
    value
  }
}

/**
 * order by
 */
export function orbCondition(code, value) {
  return {
    code,
    operator: 'ORB',
    value
  }
}

/**
 * between
 */
export function betCondition(code, value) {
  return {
    code,
    operator: 'BET',
    value
  }
}

/**
 * count
 */
export function couCondition(code, value) {
  return {
    code,
    operator: 'COU',
    value
  }
}

/**
 * 需要转换UTF-8 =
 */
export function uequCondition(code, value) {
  return {
    code,
    operator: 'U_EQU',
    value
  }
}

/**
 * 既需要转换又不转换UTF-8 =
 */
export function uaequCondition(code, value) {
  return {
    code,
    operator: 'UA_EQU',
    value
  }
}

/**
 * =
 */
export function equCondition(code, value) {
  return {
    code,
    operator: 'EQU',
    value
  }
}

/**
 * 需要转换utf-8 %
 */
export function ucopCondition(code, value) {
  return {
    code,
    operator: 'U_COP',
    value
  }
}

/**
 * 既需要转换又不转换utf-8 %
 */
export function uacopCondition(code, value) {
  return {
    code,
    operator: 'UA_COP',
    value
  }
}

/**
 * %
 */
export function copCondition(code, value) {
  return {
    code,
    operator: 'COP',
    value
  }
}

/**
 * 需要转换UTF-8 %=
 */
export function uqueCondition(code, value) {
  return {
    code,
    operator: 'U_QUE',
    value
  }
}

/**
 * 需要转换又不需要转化UTF-8 %=
 */
export function uaqueCondition(code, value) {
  return {
    code,
    operator: 'UA_QUE',
    value
  }
}

/**
 * %=
 */
export function queCondition(code, value) {
  return {
    code,
    operator: 'QUE',
    value
  }
}

/**
 * >
 */
export function grtCondition(code, value) {
  return {
    code,
    operator: 'GRT',
    value
  }
}

/**
 * <
 */
export function letCondition(code, value) {
  return {
    code,
    operator: 'LET',
    value
  }
}

/**
 * >=
 */
export function geqCondition(code, value) {
  return {
    code,
    operator: 'GEQ',
    value
  }
}

/**
 * <=
 */
export function leqCondition(code, value) {
  return {
    code,
    operator: 'LEQ',
    value
  }
}

/**
 * #
 */
export function conCondition(code, value) {
  return {
    code,
    operator: 'CON',
    value
  }
}

/**
 * /NEAR N
 */
export function neaCondition(code, value) {
  return {
    code,
    operator: 'NEA',
    value
  }
}

/**
 * /PREV N
 */
export function preCondition(code, value) {
  return {
    code,
    operator: 'PRE',
    value
  }
}

/**
 * /AFT N
 */
export function aftCondition(code, value) {
  return {
    code,
    operator: 'AFT',
    value
  }
}

/**
 * /SEN N
 */
export function senCondition(code, value) {
  return {
    code,
    operator: 'SEN',
    value
  }
}

/**
 * /PRG N
 */
export function prgCondition(code, value) {
  return {
    code,
    operator: 'PRG',
    value
  }
}

/**
 * $ N
 */
export function leoCondition(code, value) {
  return {
    code,
    operator: 'LEO',
    value
  }
}

/**
 * 不等于
 */
export function nequCondition(code, value) {
  return {
    code,
    operator: 'NEQU',
    value
  }
}

/**
 * 需要转换utf-8 不等于
 */
export function unequCondition(code, value) {
  return {
    code,
    operator: 'U_NEQU',
    value
  }
}

/**
 * 既需要转换又不转换utf-8 不等于
 */
export function uanequCondition(code, value) {
  return {
    code,
    operator: 'UA_NEQU',
    value
  }
}

/**
 * 不转换 % or %=
 */
export function mchCondition(code, value) {
  return {
    code,
    operator: 'MCH',
    value
  }
}

/**
 * 需要转换utf-8 %or%=
 */
export function umchCondition(code, value) {
  return {
    code,
    operator: 'U_MCH',
    value
  }
}

/**
 * 既要转换又不转换 % or %=
 */
export function uamchCondition(code, value) {
  return {
    code,
    operator: 'UA_MCH',
    value
  }
}

/**
 * 返回一个viewField
 */
export function getViewField(code) {
  return {
    code
  }
}

/**
 * 返回一个数组viewField
 */
export function getViewFieldArr(code) {
  return [{
    code
  }]
}

/**
 * @param {String} codes  '1', '2'
 * 返回多个code数组
 */
export function getViewFieldsArr(...codes) {
  const finalArr = []
  codes.forEach(v => {
    finalArr.push({ code: v })
  })
  return finalArr
}

export function getMinViewField(code) {
  return {
    aggreFunc: 'MIN',
    code
  }
}

export function getMaxViewField(code) {
  return {
    aggreFunc: 'MAX',
    code
  }
}

export function getSumViewField(code) {
  return {
    aggreFunc: 'SUM',
    code
  }
}

export function getAvgViewField(code) {
  return {
    aggreFunc: 'AVG',
    code
  }
}

/**
 * 添加condition
 * @param {Array} originalCondition 原始Condition
 * @param {Array} newCondition 新的Condition
 */
export function addCondition(originalCondition, newCondition) {
  if (!Array.isArray(originalCondition) || !Array.isArray(newCondition)) {
    return []
  }
  return originalCondition.concat(newCondition)
}

/**
 * 删除condition中指定code的condition
 * @param {*} condition
 * @param {*} code
 */
export function deleteConditionByCode(condition, code) {
  if (!Array.isArray(condition) || !condition.length) {
    return []
  }
  return condition.filter(c => {
    return c.code !== code
  })
}

/**
 * 批量删除condition中指定code的condition
 * @param {*} condition
 * @param {*} code
 */
export function deleteConditionByCodes(condition, codes) {
  if (!Array.isArray(condition) || !condition.length || !Array.isArray(codes)) {
    return []
  }
  return condition.filter(c => {
    return codes.indexOf(c.code) < 0
  })
}

/**
 * 根据指定的code更新condition中的值(目前仅支持单个code，有需要再改善)
 * @param {*} condition
 * @param {*} code
 * @param {*} value
 */
export function updateConditionByCode(condition, code, value) {
  if (!Array.isArray(condition) || !condition.length) {
    return []
  }
  condition.map(c => {
    if (c.code === code) {
      c.value = value
    }
  })
  return condition
}
